<template>
	<div>
		<el-dialog
			:title="title"
			:visible.sync="dialogVisibles"
			append-to-body
			style="overflow: auto;height: 100%;"
			width="790px"
			@close="closeDialog()">
			<table-form
				ref="crmform" 
				:field-list="FieldList"
				:crm-rules="crmRules" 
				:form-data="formData" 
				:crm-type="crmType" 
				:action="action.type">
			</table-form>
			<span
				slot="footer" 
				class="dialog-footer">
				<el-button 
					type="primary" 
				@click="submit()">确 定</el-button>
				<el-button 
					@click="closeDialog()">取 消</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import { TableForm } from '@/components/FromData'
	// API
	import { filedGetField } from '@/api/setting/setting'
	export default {
		data() {
			return {
				loading:false,
				// 自定义字段信息表单
				FieldList: [],
				// 自定义字段验证规则
				crmRules: {},
				// 控制dialog显示
				dialogVisibles:false
			}
		},
		props: {
			// CRM类型
			crmType: {
				type: String,
				default: ''
			},
			// 控制dialog显示
			dialogVisible:{
				type: Boolean,
				default: false
			},
			// dialog标题
			title: {
				type: String,
				default: ''
			},
			// 表单数据
			formData: {
				type: Object,
				default: () => {
					return {}
				}
			},
			// 表单数据
			fieldsData: {
				type: Array,
				default: () => {
					return []
				}
			},
			/**
			 * save:添加、update:编辑(action_id)、read:详情、index:列表
			 * relative: 相关 添加(目前用于客户等相关添加)
			 */
			action: {
				type: Object,
				default: () => {
					return {
						type: 'save',
						id: ''
					}
				}
			},
		},
		components: {
			TableForm
		},
		computed: {
			
		},
		watch: {
			crmType: function(value) {
				this.crmRules = {}
				this.FieldList = []
				// this.getField()
			},
			dialogVisible: function(value) {
				this.dialogVisibles = value
				if(value == true) this.getField()
			}
			
		},
		mounted() {},
		methods:{
			// 获取自定义字段
			getField() {
				this.loading = true
				// 获取自定义字段的更新时间
				var params = {}
				params.types = 'crm_'+this.crmType
				params.module = 'crm'
				params.controller = this.controller ? this.controller : this.crmType
				params.action = this.action.type
				// 进行编辑操作
				if (this.action.type == 'update' || this.action.id) {
					params.action_id = this.action.id
				}
				
				if (this.action.field) {
					params.field = this.action.field
				}
				
				if (this.action.actiontype) {
					params.actiontype = this.action.actiontype
				}
			
				filedGetField(params)
					.then(res => {
						let result = res.data
						// if(this.crmType == "customer"){
						// 	switch (this.action.relevant){
						// 		case "customer":
						// 			result = result.a ? result.a : result
						// 			break;
									
						// 		case "nocharge":
						// 			result = result.a ? result.a : result
						// 			break;
									
						// 		case "contacts":
						// 			result = result.b ? result.b : result
						// 			break;
						// 	}
						// }else if(this.crmType == "lawcase" || this.crmType == "contract" || this.crmType == "finance"){
						// 	result = result.a ? result.a : result
						// }
						this.FieldList = result
						// 获取表单规则
						this.getFormRules(result)
						this.loading = false
					})
					.catch(() => {
						this.loading = false
					})
			},
			
			// 获取表单规则
			getFormRules(addFormList) {
				for (let i = 0; i < addFormList.length; i++) {
					let item = addFormList[i]
					this.crmRules[item.field] = [{
						required: item.is_null ? true : false,
						message: item.name + '不能为空',
						trigger: item.form_type == 'checkbox' || item.form_type == 'select' ? ['change'] : [
							'blur'],
					}]
				}
			},
			
			// 关闭dialog弹窗
			closeDialog(){
				this.$emit('close',false)
				this.crmRules = {}
				this.dialogVisibles = false
			},
			
			// 提交
			submit(){
				let checked = this.$refs['crmform'].validateForm()
				if (checked.flag) {
					this.$emit('save-dialog',{
						"type":this.action.type + "_"+ this.action.relevant,
						"data":checked.data
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.forminput{
		width: 200px;
	}
	/deep/ .el-dialog{
		overflow: auto;
	}
</style>
